﻿Public Class LoginForm1

    ' TODO : insérez le code pour effectuer une authentification personnalisée à l'aide du nom d'utilisateur et du mot de passe fournis 
    ' (Voir http://go.microsoft.com/fwlink/?LinkId=35339).  
    ' L'objet Principal personnalisé peut ensuite être associé à l'objet Principal du thread actuel comme suit : 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' CustomPrincipal est l'implémentation IPrincipal utilisée pour effectuer l'authentification. 
    ' Par la suite, My.User retournera les informations d'identité encapsulées dans l'objet CustomPrincipal
    ' telles que le nom d'utilisateur, le nom complet, etc.

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        'create .ini file



        Dim folder As String = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData
        Dim GestionIni As New ComINI
        GestionIni.SetSection(folder + "\InnovAras.ini", "Connection_Settings", "")
        GestionIni.SetCle(folder + "\InnovAras.ini", "Connection_Settings", "User", UsernameTextBox.Text)
        GestionIni.SetCle(folder + "\InnovAras.ini", "Connection_Settings", "Pass", getMD5Hash(PasswordTextBox.Text))
        GestionIni.SetCle(folder + "\InnovAras.ini", "Connection_Settings", "DB", TextBoxDB.Text)



        Me.Close()
    End Sub

    ''' <summary>
    ''' Get the MD5 Hash of a String
    ''' </summary>
    ''' <param name="strToHash">The String to Hash</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function getMD5Hash(ByVal strToHash As String) As String
        Dim md5Obj As New Security.Cryptography.MD5CryptoServiceProvider
        Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)

        bytesToHash = md5Obj.ComputeHash(bytesToHash)

        Dim strResult As String = ""

        For Each b As Byte In bytesToHash
            strResult += b.ToString("x2")
        Next

        Return strResult
    End Function



    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

    Private Sub LoginForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim folder As String = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData
        Dim GestionIni As New ComINI
        Try

            UsernameTextBox.Text = GestionIni.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "User")
            PasswordTextBox.Text = ""
            TextBoxDB.Text = GestionIni.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "DB")
        Catch ex As Exception

        End Try
    End Sub
End Class
